var categoryId;
var categoryName;
var answerId;
var userId;
var questionId;
$(document).ready($(function() {
	var box = $('#loginBox');
	var form = $('#loginForm');
	form.mouseup(function() {
		return false;
	});
	$(this).mouseup(function(loginBox) {
		if (($(loginBox.target).parent('#loginButton').length > 0)) {
			box.show();
		}
		if (!($(loginBox.target).parent('#loginButton').length > 0)) {
			box.hide();
		}
	});
}));
$(document).ready(function() {
	$.validator.addMethod("emailRegex", function(value, element) {
		return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]+$/i.test(value);
	});
	$.validator.addMethod("loginRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F0-9]+[-_]?)*$/i.test(value);
	});
	$.validator.addMethod("passwordRegex", function(value, element) {
		return this.optional(element) || /^[a-zA-Z\u0400-\u052F0-9_-]+$/i.test(value);
	});
	$.validator.addMethod("firstNameRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F]+[-// /']?)*$/i.test(value);
	});
	$.validator.addMethod("lastNameRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F]+[-// /']?)*$/i.test(value);
	});
	$("#account-data-form").validate({
		onkeyup : function(element, event) {
			if (event.which === 9) {
				return;
			} else if ($(this).val(element) === "") {

			} else {
				this.element(element);
			}
		},
		rules : {
			inputLogin : {
				required : true,
				minlength : 4,
				maxlength : 25,
				loginRegex : true
			},
			inputEmail : {
				required : true,
				emailRegex : true
			},
			inputFName : {
				minlength : 2,
				maxlength : 30,
				firstNameRegex : true
			},
			inputLName : {
				minlength : 2,
				maxlength : 30,
				lastNameRegex : true
			},
			inputPassword : {
				required : true,
				minlength : 8,
				maxlength : 16,
				passwordRegex : true
			},
			inputConfirmPassword : {
				required : true,
				minlength : 8,
				maxlength : 16,
				passwordRegex : true,
				equalTo : "#inputPassword"
			}
		},
		messages : {
			inputLogin : {
				required : "Please enter your login.",
				minlength : "The login must be at least 4 characters long.",
				maxlength : "The login must be at least than 25 characters long.",
				loginRegex : "The login must contain only letters, numbers, dashes or hyphens."
			},
			inputEmail : {
				required : "Please enter your email.",
				emailRegex : "Please enter a valid email address."
			},
			inputPassword : {
				required : "Please enter your password",
				minlength : "The password must be more than 8 characters long",
				maxlength : "The password name must be less than 16 characters long.",
				passwordRegex : "The password must contain only letters, numbers, dashes or hyphens."
			},
			inputConfirmPassword : {
				required : "Please repeat your password",
				minlength : "The password must be more than 8 characters long.",
				maxlength : "The password name must be less than 16 characters long.",
				passwordRegex : "The password must contain only letters, numbers, dashes or hyphens.",
				equalTo : "Please enter the same password."
			},
			inputFName : {
				minlength : "The first name must be more than 2 characters long.",
				maxlength : "The first name must name must be less than 30 characters long.",
				firstNameRegex : "The first name must contain only contain only letters,apostrophe or hyphen."
			},
			inputLName : {
				minlength : "The last name must be more than 2 characters long.",
				maxlength : "The last name must be name must be less than 30 characters long.",
				lastNameRegex : "The last name must contain only letters,apostrophe or hyphen."
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').removeClass('has-success has-feedback').addClass('has-error has-feedback');
		},
		success : function(element) {
			$(element).closest('.form-group').removeClass('has-error has-feedback').addClass('has-success has-feedback');
		},
		submitHandler : function(form) {
			form.submit();
		}
	});
});
$(document).ready(function() {
	$("#inputLogin").on('focusout submit', function() {
		var $label = $('label[for="inputLogin"][class="error"]');
		if (!!$label.length == false || $label.text().length == 0) {
			var inputLogin = $(this).val();
			$(".statusLogin").html("Checking availability...");
			$.ajax({
				type : "POST",
				url : "checkAvailability",
				data : "inputLogin=" + inputLogin,
				success : function(msg) {
					$(".statusLogin").html("");
					var $test = msg.trim();
					if ($test == '1') {
						$(".statusLogin").removeClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusLogin").parent().parent().removeClass("has-error has-feedback");
						$(".statusLogin").addClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusLogin").parent().parent().addClass("has-success has-feedback");
					} else {
						$(".statusLogin").removeClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusLogin").parent().parent().removeClass("has-success has-feedback");
						$(".statusLogin").addClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusLogin").parent().parent().addClass("has-error has-feedback");
						$label.text("Not available.");
					}
				}
			});
		} else {
			$(".statusLogin").removeClass("glyphicon glyphicon-ok form-control-feedback");
			$(".statusLogin").parent().parent().removeClass("has-success has-feedback");
			$(".statusLogin").addClass("glyphicon glyphicon-remove form-control-feedback");
			$(".statusLogin").parent().parent().addClass("has-error has-feedback");
		}
	});
});
$(document).ready(function() {
	$("#inputEmail").on('focusout submit', function() {
		var $label = $('label[for="inputEmail"][class="error"]');
		if (!!$label.length == false || $label.text().length == 0) {
			var inputEmail = $(this).val();
			$(".statusEmail").html("Checking availability...");
			$.ajax({
				type : "POST",
				url : "checkAvailability",
				data : "inputEmail=" + inputEmail,
				success : function(msg) {
					$(".statusEmail").html("");
					var $test = msg.trim();
					if ($test == '1') {
						$(".statusEmail").removeClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusEmail").parent().parent().removeClass("has-error has-feedback");
						$(".statusEmail").addClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusEmail").parent().parent().addClass("has-success has-feedback");
					} else {
						$(".statusEmail").removeClass("glyphicon glyphicon-ok form-control-feedback");
						$(".statusEmail").parent().parent().removeClass("has-success has-feedback");
						$(".statusEmail").addClass("glyphicon glyphicon-remove form-control-feedback");
						$(".statusEmail").parent().parent().addClass("has-error has-feedback");
						$label.text("Not available.");
					}
				}
			});
		} else {
			$(".statusEmail").removeClass("glyphicon glyphicon-ok form-control-feedback");
			$(".statusEmail").parent().parent().removeClass("has-success has-feedback");
			$(".statusEmail").addClass("glyphicon glyphicon-remove form-control-feedback");
			$(".statusEmail").parent().parent().addClass("has-error has-feedback");
		}
	});
});

$(document).ready(function() {
	$.validator.addMethod("cNameRegex", function(value, element) {
		return this.optional(element) || /^([a-zA-Z\u0400-\u052F0-9]+[-\s// /']?)*$/i.test(value);
	});
	$("#addCategory").validate({
		onkeyup : function(element, event) {
			if (event.which === 9) {
				return;
			} else if ($(this).val(element) === "") {

			} else {
				this.element(element);
			}
		},
		rules : {
			cName : {
				required : true,
				minlength : 2,
				maxlength : 30,
				cNameRegex : true
			}
		},
		messages : {
			cName : {
				minlength : "The category name must be more than 2 characters long.",
				maxlength : "The category name must be less than 30 characters long.",
				cNameRegex : "Please enter a valid category name."
			}
		},
		submitHandler : function(form) {
			form.submit();
		}
	});
});
function openQuestion(qId) {
	$('#questionId').val(qId.trim());
	$('#showQuestionAnswers').submit();
};
function moderatorCategoriesClick() {
	$('#parent_popup').show();
	$('#moderatorCategories').show();
};
function moderatorCategoriesClose() {
	$('#parent_popup').hide();
	$('#moderatorCategories').hide();
};
function deleteCategory() {
	var confirm = $('#confirmDelete');
	confirm.hide();
	$.ajax({
		type : "POST",
		url : "removeCategory",
		data : "cId=" + categoryId,
		success : function() {
			$("td[id=" + categoryId + "]").remove();
		}
	});
	window.location.href = "pmain";
};
function confirmDeleteCategory(cId, cName) {
	$('#confirmDelete').show();
	$('#parent_popup').show();
	categoryId = cId;
	categoryName = cName;
	document.getElementById("catName").innerHTML = categoryName;

};
function confirmDeleteCategoryClose() {
	$('#parent_popup').hide();
	$('#confirmDelete').hide();
};
function likeQuestionUp(qId, thisTag) {
	var box = $('#loginBox');
	var cls = "question";
	$.ajax({
		type : "POST",
		url : "increaseLikeQuestion",
		data : "questionId=" + qId,
		success : function(msg) {
			var $test = msg.trim();
			if ($test == "") {
				box.toggle();
			} else if ($.isNumeric($test)) {
				$(thisTag).next().next().tooltip('destroy');
				$(thisTag).removeAttr('title');
				$(thisTag).next().next().removeAttr('title');
				$("span[id='" + qId + "'][class='" + cls + "']").text(msg);
			} else {
				$(thisTag).prop('title', $test);
				$(thisTag).tooltip('show');
			}
		}
	});
};
function likeQuestionDown(qId, thisTag) {
	var box = $('#loginBox');
	var cls = "question";
	$.ajax({
		type : "POST",
		url : "decreaseLikeQuestion",
		data : "questionId=" + qId,
		success : function(msg) {
			var $test = msg.trim();
			if ($test == "") {
				box.toggle();
			} else if ($.isNumeric($test)) {
				$(thisTag).prev().prev().tooltip('destroy');
				$(thisTag).removeAttr('title');
				$(thisTag).prev().prev().removeAttr('title');
				$("span[id='" + qId + "'][class='" + cls + "']").text(msg);
			} else {
				$(thisTag).prop('title', $test);
				$(thisTag).tooltip('show');
			}
		}
	});
};
function likeAnswerUp(aId, thisTag) {
	var box = $('#loginBox');
	var cls = "answer";
	$.ajax({
		type : "POST",
		url : "increaseLikeAnswer",
		data : "answerId=" + aId,
		success : function(msg) {
			var $test = msg.trim();
			if ($test == "") {
				box.toggle();
			} else if ($.isNumeric($test)) {
				$(thisTag).next().next().tooltip('destroy');
				$(thisTag).removeAttr('title');
				$(thisTag).next().next().removeAttr('title');
				$("span[id='" + aId + "'][class='" + cls + "']").text(msg);
			} else {
				$(thisTag).prop('title', $test);
				$(thisTag).tooltip('show');
			}
		}
	});
};
function likeAnswerDown(aId, thisTag) {
	var box = $('#loginBox');
	var cls = "answer";
	$.ajax({
		type : "POST",
		url : "decreaseLikeAnswer",
		data : "answerId=" + aId,
		success : function(msg) {
			var $test = msg.trim();
			if ($test == "") {
				box.toggle();
			} else if ($.isNumeric($test)) {
				$(thisTag).prev().prev().tooltip('destroy');
				$(thisTag).removeAttr('title');
				$(thisTag).prev().prev().removeAttr('title');
				$("span[id='" + aId + "'][class='" + cls + "']").text(msg);
			} else {
				$(thisTag).prop('title', $test);
				$(thisTag).tooltip('show');
			}
		}
	});
};
function changeLang(lang) {
	$('#lang').val(lang);
	$('#i18n').submit();
};
$(window).scroll(function() {
	if ($(this).scrollTop() != 0) {
		$('#goTop').fadeIn();
	} else {
		$('#goTop').fadeOut();
	}
});
function goTop() {
	$('body,html').animate({
		scrollTop : 0
	}, 800);
};
function showModeratorCategory(uID) {
	$('#selectModeratorCategory').show();
	$('#parent_popup').show();
	$('#uID').val(uID);
};
function selectModeratorCategoryClose() {
	$('#parent_popup').hide();
	$('#selectModeratorCategory').hide();
};
function log() {
	var box = $('#loginBox');
	var login = $('#userLogin').val();
	var password = $('#userPassword').val();
	params = {
		userLogin : login,
		userPassword : password
	};
	$.ajax({
		type : "POST",
		url : "login",
		data : params,
		success : function(result) {
			if (result.length > 2) {
				$(".loginStatus").html(result);
			} else {
				box.hide();
				location.reload();
			}
		}
	});
};
function feedBackClick() {
	$('#parent_popup').show();
	$('#feedBack').show();
};
function feedBackClose() {
	$('#parent_popup').hide();
	$('#feedBack').hide();
};
function deleteMessage(mId) {
	$.ajax({
		type : "POST",
		url : "deleteMessage",
		data : "mId=" + mId,
		success : function() {
			$("tr[id=" + mId + "]").remove();
			var trs = document.getElementById('container').getElementsByTagName('TR');
			if (trs.length <= 1) {
				location.reload();
			}
		}
	});
};
function messageIsRead(mId) {
	$.ajax({
		type : "POST",
		url : "isReadMessage",
		data : "mId=" + mId,
		success : function() {
			if (window.location.href.indexOf("ureadMessages") > -1) {
				$("tr[id=" + mId + "]").remove();
			} else {
				$("td[id=" + mId + "]").hide();
			}
			var trs = document.getElementById('container').getElementsByTagName('TR');
			if (trs.length <= 1) {
				location.reload();
			}
		}
	});
};
function deleteQuestion() {
	$('#confirmDeleteQuestion').hide();
	$('#parent_popup').hide();
	$.ajax({
		type : "POST",
		url : "moderatorRemoveQuestion",
		data : "qId=" + questionId,
		success : function() {
			$("tr[id=" + questionId + "]").remove();
		}
	});
};
function confirmDeleteQuestion(qId) {
	$('#confirmDeleteQuestion').show();
	$('#parent_popup').show();
	questionId = qId;
};
function confirmDeleteQuestionClose(qId) {
	$('#confirmDeleteQuestion').hide();
	$('#parent_popup').hide();
};
function deleteCurrentQuestion() {
	$('#confirmDeleteQuestion').hide();
	$('#parent_popup').hide();
	$.ajax({
		type : "POST",
		url : "moderatorRemoveQuestion",
		data : "qId=" + questionId,
		success : function() {
			$("tr[id=" + questionId + "]").remove();
		}
	});
	var oldURL = document.referrer;
	window.location.assign(oldURL);
};
function removeAnswer() {
	$('#confirmDeleteAnswer').hide();
	$('#parent_popup').hide();
	$.ajax({
		type : "POST",
		url : "moderatorRemoveAnswer",
		data : "aId=" + answerId,
		success : function() {
			$("tr[id=" + answerId + "]").remove();
		}
	});
};
function confirmDeleteAnswer(aId) {
	$('#confirmDeleteAnswer').show();
	$('#parent_popup').show();
	answerId = aId;
};
function confirmDeleteAnswerClose(aId) {
	$('#confirmDeleteAnswer').hide();
	$('#parent_popup').hide();
};
function confirmAccountClose(aId) {
	$('#confirmAccount').hide();
	$('#parent_popup').hide();
};
function addCategoryResultClose(aId) {
	$('#addCategoryResult').hide();
	$('#parent_popup').hide();
	location.reload();
};
function registerErrorClose(aId) {
	$('#registerError').hide();
	$('#parent_popup').hide();
	history.go(-1);
};
function showQuestionCategory(qID) {
	$('#selectQuestionCategory').show();
	$('#parent_popup').show();
	$('#qID').val(qID);
};
function showQuestionCategoryClose() {
	$('#parent_popup').hide();
	$('#selectQuestionCategory').hide();
};
function addBanTimeQuestion(qId, uId) {
	userId = uId;
	questionId = qId;
	$('#banTimeQuestion').show();
	$('#parent_popup').show();
};
function banUserQuestion() {
	$('#banTimeQuestion').hide();
	$('#parent_popup').hide();
	var $timeId = $('#timeIdQ');
	params = {
		usId : userId,
		qId : questionId,
		time : $timeId.val()
	};
	$.ajax({
		type : "POST",
		url : "banQuestion",
		data : params,
	});
	deleteOneQuestion(questionId);
};
function deleteOneQuestion(qId) {
	$.ajax({
		type : "POST",
		url : "moderatorRemoveQuestion",
		data : "qId=" + qId,
		success : function() {
			$("tr[id=" + qId + "]").remove();
		}
	});
	var oldURL = document.referrer;
	window.location.assign(oldURL);
};
function banUserQuestionClose() {
	$('#banTimeQuestion').hide();
	$('#parent_popup').hide();
};
function addBanTime(uId, aId) {
	userId = uId;
	answerId = aId;
	$('#banTime').show();
	$('#parent_popup').show();
};
function banUser() {
	$('#banTime').hide();
	$('#parent_popup').hide();
	var $timeId = $('#timeId');
	deleteAnswer(answerId);
	params = {
		usId : userId,
		aId : answerId,
		time : $timeId.val()
	};
	$.ajax({
		type : "POST",
		url : "ban",
		data : params,
	});
};
function deleteAnswer(aId) {
	$.ajax({
		type : "POST",
		url : "moderatorRemoveAnswer",
		data : "aId=" + aId,
		success : function() {
			$("tr[id=" + aId + "]").remove();
		}
	});
};
function banUserClose() {
	$('#banTime').hide();
	$('#parent_popup').hide();
};
function addCodeToQuestion() {
	var $currentVal = $('#questionText').val();
	var $codeText = '[code] [/code]';
	$('#questionText').val($currentVal + $codeText);
};
function addCodeToAnswer() {
	var $currentVal = $('#answerText').val();
	var $codeText = '[code] [/code]';
	$('#answerText').val($currentVal + $codeText);
};
function addCodeToQuestion() {
	var $currentVal = $('#questionText').val();
	var $codeText = '[code] [/code]';
	$('#questionText').val($currentVal + $codeText);
};
function addCodeToAnswer() {
	var $currentVal = $('#answerText').val();
	var $codeText = '[code] [/code]';
	$('#answerText').val($currentVal + $codeText);
};
function addQuestionClick() {
	$('#parent_popup').show();
	$('#addQuestion').show();
};

function addQuestionClose() {
	$('#parent_popup').hide();
	$('#addQuestion').hide();
};
function addAnswerClick() {
	$('#parent_popup').show();
	$('#addAnswer').show();
};

function addAnswerClose() {
	$('#parent_popup').hide();
	$('#addAnswer').hide();
};
function selectCategory(cName) {
	$('#categoryName').val(cName.trim());
	$('#showCategoryQuestion').submit();
};
function addCategoryBoxShow() {
	$('#addCategoryBox').show();
	$('#parent_popup').show();
};
function removeCategoryBoxShow() {
	$('#parent_popup').show();
	$('#removeCategoryBox').show();
};
function addCategoryBoxClose() {
	$('#addCategoryBox').hide();
	$('#parent_popup').hide();
};
function removeCategoryBoxClose() {
	$('#parent_popup').hide();
	$('#removeCategoryBox').hide();
};

